package com.example.demo.controller;

import com.example.demo.data.ResultDate;
import com.example.demo.pojo.Menu;
import com.example.demo.service.ResourceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("resource")
@Transactional  //开启事务回滚
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    //多条件分页查询
    @GetMapping("pageInfoByName")
    public ResultDate<Menu> getPageInfoByName(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                                              @RequestParam("menuName") String menuName){

        PageInfo<Menu> menuPageInfo = resourceService.findByName(pageNum, pageSize, menuName);

        if (menuPageInfo != null){

            ResultDate<Menu> resultDate = new ResultDate<>();
            resultDate.setPageInfo(menuPageInfo);
            return resultDate;

        }

        return null;
    }

    //资源唯一性检查
    @GetMapping("compare")
    public ResultDate<Menu> compare(@RequestParam("menuName") String menuName){
        Menu menu = new Menu();
        menu.setM_chineseName(menuName);

        List<Menu> cName = resourceService.getCName(menu);
        ResultDate<Menu> resultDate = new ResultDate<>();
        if (cName.size() >= 1){
            resultDate.setStatus(-1);
            resultDate.setMsg("资源名冲突，请重新输入资源名");
            resultDate.setList(cName);
        }else {
            resultDate.setStatus(1);
            resultDate.setMsg("资源名可以使用~");
            resultDate.setList(cName);
        }


        return resultDate;
    }

    //添加资源
    @PostMapping("addInfo")
    public ResultDate<Object> addMenu( @RequestParam("menuName") String menuName,
                                       @RequestParam("menuURL") String menuURL,
                                       @RequestParam("status") int state){

        Menu menu = new Menu();

        menu.setM_chineseName(menuName);
        menu.setM_name(menuURL);
        menu.setM_state(state);

        int flag = resourceService.addMenu(menu);

        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag>=1){
            resultDate.setMsg("添加资源成功~");
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("添加资源失败！");
        }

        return resultDate;

    }

    //根据id获取数据
    @PostMapping("getInfoById")
    public Menu getInfoById(@RequestParam("id")int id){
        return resourceService.getInfoById(id);
    }


    //更新资源
    @PostMapping("update")
    public ResultDate<Object> updateMenu(@RequestParam("id")int id,
                                         @RequestParam("menuName") String menuName,
                                         @RequestParam("menuURL") String menuURL,
                                         @RequestParam("status") int state){

        int flag = resourceService.updateMemu(id, menuName, menuURL, state);
        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag >=1 ){
            resultDate.setMsg("数据更新成功~");
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("数据更新失败！请重试");
        }

        return resultDate;
    }

    //删除资源
    @PostMapping("delete")
    public ResultDate<Object> delete(@RequestParam("id")int id){

        int flag = resourceService.delete(id);
        ResultDate<Object> resultDate = new ResultDate<>();
        if (flag >=1 ){
            resultDate.setMsg("数据删除成功~");
        }else {
            resultDate.setStatus(-1);
            resultDate.setMsg("数据删除失败！请重试");
        }

        return resultDate;
    }






}
